
package com.compuvest.admin.update.v1

class Item implements Serializable {

    final static int MAX_WORD_WIDTH = 25

    def category = [:], vendor = [:], field = [:], fdesc = [:], pref = [:], advert = [:], banner = [:]
    def utils = [:], json = [:], images = [:], history = [:], needupdate = [:], quantity = [:]
    def result, resulttype, resultmessage

    void setResult(_result, _resulttype, _resultmessage) {
        result = _result
        resulttype = _resulttype
        if(! resultmessage) { //empty
            resultmessage = _resultmessage
        }
        //resultmessage = _resultmessage
    }

    // prepare search keywords
    String getKeywords() {

        def thesaurus = new StringBuilder()
        thesaurus << fdesc['etilid']
        thesaurus << " ${utils['accwareid']}"
        thesaurus << " ${utils['accwareid'].replaceAll(/\-../, '')}"     // 452001880-00 -> 452001880
        thesaurus << " ${field['mfrname']}"
        thesaurus << " ${field['mfrnumber']}"
        thesaurus << " ${field['mfrnumber'].replaceAll(/\#.*/, '')}"    // ABA#777 -> ABA
        thesaurus << " ${advert['advmfrname']}"
        thesaurus << " ${advert['advmfrnumber'].replaceAll(/\#.*/, '')}" // ABA#777 -> ABA
        thesaurus << " ${field['itemname']}"
        thesaurus << " ${field['advname']}"
        thesaurus << " ${advert['keywords']}"
        thesaurus << " ${json['namea']}"
        thesaurus << " ${json['nameb']}"
        thesaurus << " ${json['pkeywrd']}"

        return swords(thesaurus.toString().toLowerCase()).toString().replaceAll(/\,|\[|\]|\#|\/|\.|\-|\(|\)/, '') // delete , [ ] # / . - ( )
    }

    def swords(s) {

        def result = []
	s.split(/\p{Space}/).each { word ->

            if(word.length() < MAX_WORD_WIDTH) {

		if("hp" == word) word = "hpp"

                if(word.length() > 2) {
                    result += word
                }
            }
        }

	return result as Set // only unique words
    }

    String getJSON() {

        def js = new StringBuilder()
        js << "{"
        js << "type:\"E\","
        js << "eid:\"${fdesc['etilid']}\","
        js << "sku:\"${utils['accwareid']}\","
        js << "name:\"${field['itemname']}\","
        js << "advname:\"${field['advname']}\","
        js << "namea:\"${json['namea']}\","
        js << "nameb:\"${json['nameb']}\","
        js << "mfrname:\"${field['mfrname']}\","
        js << "mfrnumber:\"${field['mfrnumber']}\","
        js << "qty:${quantity['qty']},"
        js << "stock:\"${field['stock']}\","
        js << "sprc:${field['sellprice']},"
        js << "hprc:${field['reslprice']},"
        js << "r1prc:${field['reslprice1']},"
        js << "r2prc:${field['reslprice2']},"
        js << "r3prc:${field['reslprice3']},"
        js << "r2qty:${field['reslqty2']},"
        js << "r3qty:${field['reslqty3']},"
        js << "wrmsk:${field['wtymask']},"
        js << "wr1prc:${field['wtyprice1']},"
        js << "wr2prc:${field['wtyprice2']},"
        js << "wr3prc:${field['wtyprice3']},"
        js << "mfrpn:\"${field['mfrnumber']}\","
        js << "minfo:\"${json['minfo']}\","
        js << "mspec:{${json['mspec']}},"
        js << "espec:[${json['espec']}],"
        js << "psiml:\"${json['psiml']}\","
        js << "paccl:\"${json['paccl']}\","
        js << "sattr:{${json['sattr']}},"
        js << "imgs:[${json['imgs']}],"
        js << "pdf:[${json['pdf']}]"
        js << "}"
    }

    String toString() {

        def out = new StringBuilder();

        out << "\nutils:\t${utils}"

        return out
    }

}

